package com.mk.wardsystemserver.controller;

import com.mk.wardsystemserver.pojo.Admin;
import com.mk.wardsystemserver.pojo.RespBean;
import com.mk.wardsystemserver.service.IAdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;
    @ApiOperation(value="更新当前用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        //更新用户信息
        if(adminService.updateById(admin)){
            SecurityContextHolder.getContext()
                    .setAuthentication(new UsernamePasswordAuthenticationToken(
                    admin,
                    null,
                    authentication.getAuthorities()));
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
//    更新密码
    @ApiOperation(value="更新用户密码")
    @PutMapping("/admin/password")
    public RespBean updateAdminPassword(@RequestBody Map<String,Object> info){
        String oldPassword=(String) info.get("oldPassword");
        String newPassword=(String) info.get("newPassword");
        Integer adminId=(Integer) info.get("adminId");
        return adminService.updateAdminPassword( oldPassword, newPassword, adminId);
    }
}
